PATHMac OS 8 and 9 Developer Documentation > Interapplication Communication > AppleScript for Scripters >

AppleScript Language Guide

   

The Return Statement

A Return statement allows you to stop execution of a handler before all its statements are executed and to return a value. Many of the examples in this chapter use Return statements.

A Return statement exits a handler and returns a value. When AppleScript executes a Return statement, it stops handler execution and resumes execution at the place in the script where the handler was called, using the value returned as the value of the handler.

SYNTAX
return expression

where

expression is an AppleScript expression. When AppleScript executes a Return statement, it returns the value of the expression. For related information, see Expressions.

EXAMPLE

To return a value and exit a subroutine, include a Return statement in the body of the subroutine. For example, the following statement returns the integer 2 :

return 2

If you include a Return statement without an expression, AppleScript exits the subroutine immediately and no value is returned.

NOTES

If a subroutine does not include a Return statement, AppleScript executes the statements in the subroutine and, after handling the last statement, returns the value of the last statement in the subroutine. If the last statement does not return a value, then no value is returned.

When AppleScript has finished executing a subroutine (that is, when it executes a Return statement or the last statement in the subroutine), it passes control to the place in the script immediately after the place where the subroutine was called.

In general, it is best to have just one Return statement and locate it at the end of a subroutine or handler. For most scripts, doing so provides the following benefits:

In some cases, however, it may make more sense to use multiple Return statements. For example, the minimumValue subroutine in A Sample Subroutine is a simple script that uses two return statements.


© 1999 Apple Computer, Inc. – (Last Updated 21 May 99)